/****************************************************************************
 * Copyright (C) 2012 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "ThemeDownloadTask.h"
#include "../../Controls/Application.h"
#include "../../Prompts/ProgressWindow.h"
#include "../../Network/FileDownloader.h"

ThemeDownloadTask::ThemeDownloadTask(const std::string &Url)
	: url(Url)
{
	result = false;
	running = true;
	
	ThreadedTaskHandler::Instance()->AddTask(this);
}

void ThemeDownloadTask::Execute(void)
{
	if(ProgressWindow::Instance()->IsRunning())
		ProgressWindow::Instance()->SetTitle(tr("Downloading file:"));
	else
		StartProgress(tr("Downloading file:"));

	char filepath[300];
	snprintf(filepath, sizeof(filepath), "%s/TempTheme.zip", Settings.ThemePath);
	
	int ret = DownloadFileToPath(url.c_str(), filepath, false);
	if(ret < 1024)
	{
		StopProgress();
		ShowError(tr("Error when downloading file: %i"), ret);
		running = false;
		return;
	}
	
	sleep(1);
	
	result = true;
	running = false;
}
